
import { AxiosRequestConfig } from "./types";
import { transformRequest, transformResponse } from './helpers/data';
import { processHeaders } from './helpers/headers';

/**
 * axios一些默认配置
 * transformRequest & transformResponse 对post 、put、patch请求方式有效
 * transformRequest处理用户传入参数data和请求头
 */
const defaults: AxiosRequestConfig = {
  method: 'get',
  timeout: 0,
  headers: {
    common: {
      Accept: 'application/json, text/plain, */*'
    }
  },
  
  transformRequest: [
    // 原dispatchRquest.ts中header和data逻辑移动此处
    function(data: any, headers?: any ) { 
      processHeaders(headers, data)
      return transformRequest(data)
    }
  ],

  transformResponse: [
    function(data: any): any {
      return transformResponse(data)
    }
  ],

  xsrfCookieName: 'XSRF-TOKEN',
  xsrfHeaderName: 'X-XSRF-TOKEN',

  validateStatus(status: number): boolean {
    return status >= 200 && status < 300
  }
}

const methodsNoData = ['delete', 'get', 'head', 'options']
methodsNoData.forEach(method => {
  defaults.headers[method] = {}
})


const methodsWithData = ['post', 'put', 'patch']
methodsWithData.forEach(method => {
  defaults.headers[method] = {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

export default defaults